// 创建store并导出
// -- 固定 --
import { applyMiddleware, createStore } from 'redux'
import reducer from './reducers'
import thunk, { ThunkAction } from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension'
const store = createStore(reducer, composeWithDevTools(applyMiddleware(thunk)))

// store.getState可以拿到所有的state
console.log(store.getState())

export type ToDoItem = {
    id: number,
    name: string,
    isDone: boolean
  }
export type ChannelItem = {
  id: number,
  name: string
}

export type ArticleItem = {
  art_id: string;
  title: string;
  aut_id: string;
  comm_count: number;
  pubdate: string;
  aut_name: string;
  is_top: number;
  cover: {
      type: number;
      images: string[];
  }
}

// 所有的state的集合的类型
export type RootState = ReturnType<typeof store.getState>
// 同步的action: 所有的ation的类型
// todo模块
export type TodoActionType = { type: 'todo/UPDATE_STATE', payload: number }
| {type: 'todo/ADD_STATE', payload: ToDoItem }
| {type: 'todo/DEL_STATE', id: number}
| {type: 'todo/INIT_STATE', initState: ToDoItem[]}

// channel模块
export type ChannelActionType =
| {type: 'channel/SET_CHANNELS', initState: ChannelItem[]}
| {type: 'channel/SET_CHANNEL_INDEX', payload: number}

// article模块
export type ArticleActionType =
| {type: 'article/SET_ARTICLES', initState: any[]}

export type RootActionType = TodoActionType | ChannelActionType | ArticleActionType

// 异步的action: 所有的异步的action的type
export type RootThunkAction = ThunkAction<void, RootState, unknown, RootActionType>

export default store
